package com.example.beautymakeup.controller;

import com.example.beautymakeup.domain.Code;
import com.example.beautymakeup.domain.PageBean;
import com.example.beautymakeup.domain.Result;
import com.example.beautymakeup.pojo.Address;
import com.example.beautymakeup.pojo.Banner;
import com.example.beautymakeup.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressService addressService;
    @GetMapping
    public Result get(@RequestHeader("Shop_token") String token) throws Exception {
        PageBean<Address> pageBean = addressService.get(token);
        if (pageBean != null){
            return new Result(Code.GET_OK,"获取成功",pageBean);
        }
        return new Result(Code.GET_ERR,"获取失败");

    }

    @GetMapping("/{uid}")
    public Result getByUid(@RequestHeader("Shop_token") String token,@PathVariable("uid") int uid) throws Exception {
        Address byUid = addressService.getByUid(token, uid);
        if (byUid != null){
            return new Result(Code.GET_OK,"获取成功",byUid);
        }
        return new Result(Code.GET_ERR,"获取失败");
    }



    @PutMapping
    public Result update(@RequestHeader("Shop_token") String token,@RequestBody Address address) throws Exception {
        boolean update = addressService.update(token, address);
        if (update){
            return new Result(Code.UPDATE_OK,"修改成功");
        }
        return new Result(Code.UPDATE_ERR,"修改失败");
    }

    @PostMapping
    public Result add(@RequestHeader("Shop_token") String token,@RequestBody Address address) throws Exception {
        boolean add = addressService.add(token, address);
        if (add){
            return new Result(Code.SAVE_OK,"增加成功");
        }
        return new Result(Code.SAVE_ERR,"增加失败");
    }



    @DeleteMapping
    public Result delete(@RequestHeader("Shop_token") String token,@RequestBody Address address) throws Exception {
        boolean delete = addressService.delete(token, address);
        if (delete){
            return new Result(Code.DELETE_OK,"删除成功");
        }
        return new Result(Code.DELETE_ERR,"删除失败");
    }
}
